<?php
/*
    Author: dkloz
    Purpose: This function adds an error to an array of errors in the SESSION
*/
require_once ('include/functions.php');

function BL_registerError($errorCode)
{
    $_SESSION['bl_errors'] = array();

    if (exists($errorCode, $_SESSION['bl_errors']) == 0) {
        array_push($_SESSION['bl_errors'], $errorCode);
    }

    return BL_OK;
}

/*
    Author: dkloz
    Improved by: basanas
    Purpose: This function returns a message with all the errors that occured
    and at the same time clears the error array
*/

function BL_flushErrors()
{
    if ( isset($_SESSION['bl_errors']) )
        $messages = $_SESSION['bl_errors'];
    else
        return;

    foreach ($messages as $error)
    {
        if ($error == BL_MAIL_ERROR) {
            UI_RegisterError("msg_error", "A mail could not be sent");
        }
        else if ($error == BL_ERROR_IN_INSERT_ENTITY) {
            UI_RegisterError("msg_error", "The entity could not be inserted");
        }
        else if ($error == BL_ERROR_IN_CATEGORY_SEARCH) {
            UI_RegisterError("msg_error", "An error occured while searching for categories");
        }
        else if ($error == BL_ERROR_IN_ENTITY_SEARCH) {
            UI_RegisterError("msg_error", "An error occured while searching for entities");
        }
        else if ( ($error == BL_ADD_CATEGORY) || ($error == BL_ADMIN_ADD_CATEGORY) ) {
            UI_RegisterError("msg_error", "Could not add category");
        }
        else if ($error == BL_ADD_ATTRIBUTE_TO_CATEGORY) {
            UI_RegisterError("msg_error", "Could not add an attribute to the category");
        }
        else if ($error == BL_GET_PENDING_CATEGORIES) {
            UI_RegisterError("msg_error", "Could not fetch pending categories");
        }
        else if ($error == BL_GET_PENDING_ENTITIES) {
            UI_RegisterError("msg_error", "Could not fetch pending entities");
        }
        else if ($error == BL_GET_USER_DATA) {
            UI_RegisterError("msg_error", "Could not fetch user's data");
        }
        else if ($error == BL_REGISTER_USER) {
            UI_RegisterError("msg_error", "Registration failed");
        }
        else if ($error == BL_USER_LOGIN) {
            UI_RegisterError("msg_error", "Login failed");
        }
        else if ($error == BL_GET_PUBLIC_CATEGORIES_ERROR) {
            UI_RegisterError("msg_error", "Could not get public categories");
        }
        else if ($error == BL_DISABLE_ENTITY) {
            UI_RegisterError("msg_error", "Could not disable entity");
        }
        else if ($error == BL_DISABLE_CATEGORY) {
            UI_RegisterError("msg_error", "The category was not disabled");
        }
        else if ( ($error == BL_ENABLE_CATEGORY) || ($error == BL_ADMIN_ENABLE_CATEGORY) ) {
            UI_RegisterError("msg_error", "Could not enable target category");
        }
        else if ($error == BL_ENABLE_ENTITY) {
            UI_RegisterError("msg_error", "Could not enable target entity");
        }
        else if ($error == BL_ERROR_IN_ACTIVATION) {
            UI_RegisterError("msg_error", "Activation failed");
        }
        else if ($error == BL_NOT_OWNER) {
            UI_RegisterError("msg_error", "You don't have the need permissions for this action");
        }
        else if ($error == BL_EDIT_DELETE_ATTR_FRM_CAT) {
            UI_RegisterError("msg_error", "Attribute could not be deleted from the category");
        }
        else if ($error == BL_EDIT_ADD_ATTR_TO_CAT) {
            UI_RegisterError("msg_error", "Attribute could not be added to the category");
        }
        else if ($error == BL_EDIT_DELETE_USR_FRM_CAT) {
            UI_RegisterError("msg_error", "User could not deleted from the category");
        }
        else if ($error == BL_EDIT_ADD_USER_TO_CAT) {
            UI_RegisterError("msg_error", "User could not be added to the category");
        }
        else if ($error == BL_EDIT_DELETE_MOD_FROM_CAT) {
            UI_RegisterError("msg_error", "Moderator could not deleted from the category");
        }
        else if ($error == BL_EDIT_ADD_MOD_TO_CAT) {
            UI_RegisterError("msg_error", "Moderator could not be added to the category");
        }
        else if ( ($error == BL_GET_CATEGORIES_WITH_MODERATOR) ||
                  ($error == BL_GET_CATEGORIES_WITH_OWNER)     ||
                  ($error == BL_GET_CATEGORIES_WITH_MEMBER)
                ) {
            UI_RegisterError("msg_error", "Error retrieving category list");
        }
        else if ($error == BL_GET_PENDING_CATEOGORIES) {
            UI_RegisterError("msg_error", "Could not get the categories pending activation");
        }
        else if ($error == BL_GET_USERS) {
            UI_RegisterError("msg_error", "Could not get user list");
        }
        else if ($error == BL_GET_USERS_PRUNED) {
            UI_RegisterError("msg_error", "Could not get user list");
        }
        else if ($error == BL_SEND_EMAIL) {
            UI_RegisterError("msg_error", "Email could not be sent to the specified user");
        }
        else if ($error == BL_USRNAME_EXISTS) {
            UI_RegisterError("msg_error", "The Username you chose already exists in our system. Please Choose another one.");
        }
        else if ($error == BL_EMAIL_EXISTS) {
            UI_RegisterError("msg_error", "The email you chose already exists in our system.");
        }
        else if($error == BL_SAME_NAME_CAT){
		  UI_RegisterError("msg_error", "The name of the category you chose already exists in our system. Please Choose another one.");
        }
        else if($error == BL_INSERT_ENT_SAME_NAME){
		  UI_RegisterError("msg_error", "The name of the entity you chose already exists in that category. Please Choose another one.");
        }
        //else if ($error == BL_USERNAME_TO_ID) {
        //    UI_RegisterError("msg_error", "Email could not be sent to the specified user");
        //}
        else {
            /* TODO: Nielsen wouldn't be proud :P */
            UI_RegisterError("msg_error", "A technical error has occured!");
        }
    }

    unset($_SESSION['bl_errors']);
}


/*
    Author: dkloz
    Purpose: Checks if an element exists in that array
    Status: Not Debbuged
*/

function exists($code, $array)
{
    $count = count($array);

    for ($i = 0; $i < $count; $i++) {
        if ($array[$i] == $code) {
            return 1;
        }
    }

    return 0;
}
?>
